package Servlet.Teacher;

import Entity.MyTableData;
import Entity.TitleAndPaper.Title;
import Service.Teacher.Imp.TitleAndPaperServiceImp;
import Service.Teacher.TitleAndPaperService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * Author: Deng PengFei
 * Date: 2021/8/22
 * Time: 18:47
 */

@WebServlet(urlPatterns = {"/getTitle", "/addTitle", "/checkTitle"},name = "TitleAndPaperServlet")
public class TitleAndPaperServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/getTitle".equals(url)) {
            getTitle(request, response);
        } else if ("/addTitle".equals(url)) {
            addTitle(request, response);
        } else if ("/checkTitle".equals(url)) {
            checkTitle(request, response);
        }

    }

    /**
     * 得到所有老师申请的课题
     */
    protected void getTitle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TitleAndPaperService titleAndPaperService = new TitleAndPaperServiceImp();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int offset = (page - 1) * limit;
        int type = Integer.parseInt(request.getParameter("type")); // 1为申请人是学生  2 申请人为老师
        String queryKey = request.getParameter("queryKey");
        if (queryKey == null || "".equals(queryKey)) {
            queryKey = "";
        }
        queryKey = "%" + queryKey + "%";
        MyTableData myTableData = titleAndPaperService.queryAllTitle(queryKey, offset, limit, type);
        String json = JSONObject.toJSONString(myTableData, SerializerFeature.WriteMapNullValue);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }

    /**
     * 加入一条课题
     */
    protected void addTitle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TitleAndPaperService titleAndPaperService = new TitleAndPaperServiceImp();
        String NO = request.getParameter("userName");
        String data = request.getParameter("taskData");
        Integer type = Integer.parseInt(request.getParameter("type"));
        JSONObject json = JSONObject.parseObject(data);
        System.out.println(json);
        Title title = new Title();
        title.setNo(NO);
        title.setTitle(json.get("title").toString());
        title.setCollegeName(json.get("college").toString());
        title.setMajorName(json.get("major").toString());
        title.setFrom(json.get("from").toString());
        title.setNature(json.get("nature").toString());
        title.setCategoty(json.get("category").toString());
        title.setIntr(json.get("taskText").toString());
        boolean flag = titleAndPaperService.addTitle(title, type);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }

    /**
     * 更新课题审核的结果
     */
    protected void checkTitle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TitleAndPaperService titleAndPaperService = new TitleAndPaperServiceImp();
        String result = request.getParameter("result");
        Integer id = Integer.parseInt(request.getParameter("id"));
        String opinion = request.getParameter("opinion");
        boolean flag = titleAndPaperService.checkTitle(result, opinion, id);
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.close();
    }


}
